//
//  HomeTableViewController.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "HomeTableViewController.h"
#import "PlayerViewController.h"
#import "MJRefresh.h"
#import "VideoCell.h"
#import "Page.h"
#import "Daily.h"

@interface HomeTableViewController ()

@property (nonatomic , strong) NSMutableArray *dailyList;

@end

@implementation HomeTableViewController


- (NSMutableArray *)dailyList
{
    if(!_dailyList)
    {
        _dailyList = [NSMutableArray array];
    }
    return _dailyList;
}

/** 设置Nav */
- (void)addNavigationItem
{
    NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
    attrs[NSFontAttributeName] = Font_ChinaBold(20);
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"每日精选" attributes:attrs];
    
    UILabel *titleView = [[UILabel alloc] init];
    titleView.attributedText = title;
    [titleView setBounds:CGRectMake(0, 0, title.size.width, title.size.height)];
    self.navigationItem.titleView = titleView;
    self.navigationItem.titleView.alpha = 0.7;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addNavigationItem];
    
    [self addRefresh];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)addRefresh
{
    // 添加下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerWithRefreshDate)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerWithRefreshingMore)];
    
}

- (void)headerWithRefreshDate
{
    __weak typeof(self) weakSelf = self;
    
    [[AFHTTPSessionManager manager] GET:[Page toPath] parameters:[Page toParameterWith:[NSDate date]] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        Page *page = [Page mj_objectWithKeyValues:responseObject];
        
        Daily *daily = [self.dailyList firstObject];
        
        NSString *dateString = [[NSDate date] formattedDateWithFormat:@"YYYYMMdd"];
        
        if(![daily.date.formatterDateToYYMMDDToDay isEqualToString:dateString])
        {
            NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, page.dailyList.count)];
            
            [weakSelf.dailyList insertObjects:page.dailyList atIndexes:indexSet];
        }
        
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)footerWithRefreshingMore
{
    Daily *lastDaily = [self.dailyList lastObject];
    __weak typeof(self) weakSelf = self;
    [[AFHTTPSessionManager manager] GET:[Page toPath] parameters:[Page toParameterWithStr:lastDaily.date.formatterDateToYYMMDDToYestday] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        Page *page = [Page mj_objectWithKeyValues:responseObject];
        
        [weakSelf.dailyList addObjectsFromArray:page.dailyList];
        
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dailyList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    Daily *daily = self.dailyList[section];
    return daily.videoList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCell *cell = [VideoCell cellWithTableView:tableView];
    
    Daily *daily = self.dailyList[indexPath.section];
    
    Video *video = daily.videoList[indexPath.row];
    
    cell.video = video;
    
    return cell;
}

#pragma mark - header
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UITableViewHeaderFooterView *header = [[UITableViewHeaderFooterView alloc] init];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *pv = [[PlayerViewController alloc] init];
    
    Daily *daily = self.dailyList[indexPath.section];
    Video *video = daily.videoList[indexPath.row];
    pv.video = video;
    
    [self presentViewController:pv animated:YES completion:nil];
}

@end
